//
//  Person.m
//  MacTest
//
//  Created by 小星星吃KFC on 2020/11/13.
//

#import "Person.h"

@implementation Dog


-  (void)setValuesForKeysWithDictionary:(NSDictionary<NSString *,id> *)keyedValues {
    self.ID = keyedValues[@"ID"];
    self.name = keyedValues[@"name"];
    self.age = [keyedValues[@"age"] integerValue];
}


- (NSString *)description {
    return [NSString stringWithFormat:@"{id:%@,name:%@,age:%ld}",
            self.ID,self.name,self.age];
}


@end
@implementation Person

-  (void)setValuesForKeysWithDictionary:(NSDictionary<NSString *,id> *)keyedValues {
    self.ID = keyedValues[@"ID"];
    self.name = keyedValues[@"name"];
    self.age = [keyedValues[@"age"] integerValue];
    
    if (keyedValues[@"dog"]) {
        self.dog = Dog.alloc.init;
        [self.dog setValuesForKeysWithDictionary:keyedValues[@"dog"]];
    }
    
    if (keyedValues[@"dogs"] && [keyedValues[@"dogs"] isKindOfClass:NSArray.class]) {
        
        NSArray *arrar = keyedValues[@"dogs"];
        NSMutableArray<Dog *> *temp = NSMutableArray.array;
        
        [arrar enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            Dog *dog = Dog.alloc.init;
            [dog setValuesForKeysWithDictionary:obj];
            [temp addObject:dog];
        }];
        self.dogArray = temp;
    }
}




- (NSString *)description {
    
    
//        NSMutableArray<NSString *> *sqlArr = NSMutableArray.array;
//
//        for (Dog *dog in self.dogArray ) {
//            NSString *str= [NSString stringWithFormat:@"dog:{id:%@,name:%@,age:%ld}",dog.ID,dog.name,dog.age];
//            [sqlArr addObject:str];
//        }
//
//        return [NSString stringWithFormat:@"{id:%@,name:%@,age:%ld,dog:{id:%@,name:%@,age:%ld},dogArray:[%@]}",
//                self.ID,self.name,self.age,self.dog.ID,self.dog.name,self.dog.age,[sqlArr componentsJoinedByString:@","]];
//
    
    return [NSString stringWithFormat:@"{id:%@,name:%@,age:%ld,dogs:[%@]}",self.ID,self.name,self.age,[self.dogArray componentsJoinedByString:@","]];
    
}


+(NSArray<Person*>* )demoArray {
    
    NSMutableArray<Person*> *temp = NSMutableArray.array;
    for (int i=0; i<5; i++) {
        
        NSDictionary *dict = @{
            @"ID":[NSString stringWithFormat:@"%d",i],
            @"name":[NSString stringWithFormat:@"person-%d",i],
            @"age":@(2),
            @"dogs":@[
                    @{
                        @"ID":[NSString stringWithFormat:@"0%d",i],
                        @"name":[NSString stringWithFormat:@"dog0-%d",i],
                        @"age":@(2),
                    },
                    @{
                        @"ID":[NSString stringWithFormat:@"1%d",i],
                        @"name":[NSString stringWithFormat:@"dog1-%d",i],
                        @"age":@(3),
                    },
            ],
        };
        
        Person *person = Person.alloc.init;
        [person setValuesForKeysWithDictionary:dict];
        [temp addObject:person];
    }
    return temp.copy;
}

+(NSArray<Person*>* )demoArray2 {
    
    NSMutableArray<Person*> *temp = NSMutableArray.array;
    for (int i=0; i<5; i++) {
        
        NSDictionary *dict = @{
            @"ID":[NSString stringWithFormat:@"%d",i],
            @"name":[NSString stringWithFormat:@"person-%d",i],
            @"age":@(2),
            @"dogs":@[
                    @{
                        @"ID":[NSString stringWithFormat:@"0%d",i],
                        @"name":@"goofy",
                        @"age":@(2),
                    },
                    @{
                        @"ID":[NSString stringWithFormat:@"1%d",i],
                        @"name":[NSString stringWithFormat:@"goofy-1-%d",i],
                        @"age":@(3),
                    },
            ],
        };
        
        Person *person = Person.alloc.init;
        [person setValuesForKeysWithDictionary:dict];
        [temp addObject:person];
    }
    return temp.copy;
}

+(void)testA:(NSString *)name {
    NSString *dealString =  [NSStringFromSelector(_cmd) stringByReplacingOccurrencesOfString:@":" withString:@""];
    
    
    dealString =  [dealString stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:[[dealString substringToIndex:1] capitalizedString]];
    NSLog(@"当前方法是： %@",dealString);
    
}


@end
